package com.example.backend.security;

import com.example.backend.entity.User;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;

/**
 * 登录用户信息
 */
@Data
public class LoginUser implements UserDetails {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 用户ID
     */
    private Long userId;
    
    /**
     * 用户名
     */
    private String username;
    
    /**
     * 密码
     */
    private String password;
    
    /**
     * 权限列表
     */
    private Set<GrantedAuthority> authorities;
    
    /**
     * 账号是否未过期
     */
    private boolean accountNonExpired = true;
    
    /**
     * 账号是否未锁定
     */
    private boolean accountNonLocked = true;
    
    /**
     * 凭证是否未过期
     */
    private boolean credentialsNonExpired = true;
    
    /**
     * 账号是否启用
     */
    private boolean enabled = true;
    
    /**
     * 创建登录用户
     */
    public static LoginUser fromUser(User user, Set<GrantedAuthority> authorities) {
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(user.getId());
        loginUser.setUsername(user.getUsername());
        loginUser.setPassword(user.getPassword());
        loginUser.setAuthorities(authorities);
        loginUser.setEnabled(user.getStatus() == 1);
        return loginUser;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }
    
    @Override
    public String getPassword() {
        return password;
    }
    
    @Override
    public String getUsername() {
        return username;
    }
    
    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }
    
    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }
    
    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }
    
    @Override
    public boolean isEnabled() {
        return enabled;
    }
} 